package com.qy127.javaweb.dao.impl;


import com.qy127.javaweb.dao.DeptDao;
import com.qy127.javaweb.entity.Dept;
import com.qy127.javaweb.util.JDBCUtil;

import java.util.ArrayList;
import java.util.List;

public class DeptDaoImpl implements DeptDao {
    /**
     * 查询
     * @return
     */
    @Override
    public List<Dept> getAllDept() {
        String sql="select * from t_dept";
        List<List> lists = JDBCUtil.queryList(sql);
        if (lists.size()>0){
            ArrayList<Dept> depts = new ArrayList<>();
            for (List list : lists) {
                Dept dept = new Dept((Integer) list.get(0), (Integer) list.get(1), (String) list.get(2), (String) list.get(3), (Integer) list.get(4));
                depts.add(dept);

            }
            return depts;
        }
        return null;
    }

    /**
     * 增加一个部门
     * @param deptNo
     * @param dname
     * @param remark
     * @param status
     * @return
     */
    @Override
    public int insertOneDept(Integer deptNo, String dname, String remark, Integer status) {
        String sql="insert into t_dept values(null,?,?,?,?)";
        int i = JDBCUtil.update(sql, deptNo, dname, remark, status);
        return i;
    }

    /**
     * 删除一个部门
     * @param deprid
     * @return
     */
    @Override
    public int deleteOneDept(Integer deprid) {
        String sql="delete from t_dept where deptid=?";
        int i = JDBCUtil.update(sql, deprid);
        return i;
    }

    /**
     * 去修改
     * @param deptid
     * @return
     */
    @Override
    public Dept goupdateDept(Integer deptid) {
        String sql = "select * from t_dept where deptid=?";
        List<List> lists = JDBCUtil.queryList(sql, deptid);
        if (lists.size() > 0) {
            List list = lists.get(0);
            Dept dept = new Dept((Integer) list.get(0), (Integer) list.get(1), (String) list.get(2), (String) list.get(3), (Integer) list.get(4));
            return dept;

        }
        return null;
    }

    /**
     * 修改
     * @param dept
     * @return
     */
    @Override
    public int updatedept(Dept dept) {
        String sql="update t_dept set deptNO=?,dname=?,remark=?,status=? where deptid=? ";
        int i = JDBCUtil.update(sql, dept.getDeptNo(), dept.getDname(), dept.getRemark(), dept.getStatus(),dept.getDeptid());
        return i;
    }
}
